﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace Mantenimiento.CAD
{
    public class Modelo
    {
        public static DataTable buscar()
        {
            dbManager db = new dbManager();

            //PARAMETROS 
            object[] parametros = new object[0];
            DataTable dt = db.EjecutarSP_DS("MODELOS_SELECT_ALL", parametros).Tables[0];
            return dt;
        }
        public static DataTable buscar(int iId)
        {
            dbManager db = new dbManager();

            //PARAMETROS 
            object[] parametros = new object[2];
            parametros[0] = "@Id";
            parametros[1] = iId;
            DataTable dt = db.EjecutarSP_DS("MODELOS_SELECT", parametros).Tables[0];
            return dt;
        }
        public static DataTable buscarxmarca(int iIdMarca)
        {
            dbManager db = new dbManager();

            //PARAMETROS 
            object[] parametros = new object[2];
            parametros[0] = "@Id";
            parametros[1] = iIdMarca;
            DataTable dt = db.EjecutarSP_DS("MODELOS_SELECT_MARCA", parametros).Tables[0];
            return dt;
        }

        public static int insertar
            (string sNombre, int iIdMarca)
        {
            int iRet = -1;
            object[] parametros = new object[4];
            parametros[0] = "@Nombre";
            parametros[1] = sNombre;
            parametros[2] = "@IdMarca";
            parametros[3] = iIdMarca;

            dbManager db = new dbManager();
            db.Conectar();

            object salida = db.EjecutarSP("MODELOS_INSERT", parametros);
            db.Desconectar();
            try
            {
                iRet = int.Parse(salida.ToString());
            }
            catch (Exception ex)
            {
                iRet = -10;
            }
            return iRet;
        }

        public static int guardar
            (int iId, string sNombre, int iIdMarca)
        {
            int iRet = -1;
            object[] parametros = new object[6];
            parametros[0] = "@Nombre";
            parametros[1] = sNombre;
            parametros[2] = "@IdMarca";
            parametros[3] = iIdMarca;
            parametros[4] = "@Id";
            parametros[5] = iId;

            dbManager db = new dbManager();
            db.Conectar();

            object salida = db.EjecutarSP("MODELOS_UPDATE", parametros);
            db.Desconectar();
            try
            {
                iRet = int.Parse(salida.ToString());
            }
            catch (Exception ex)
            {
                iRet = -10;
            }
            return iRet;
        }

        public static int eliminar
            (int iId)
        {
            int iRet = -1;
            dbManager db = new dbManager();
            string sSQL = string.Format("DELETE FROM MODELOS WHERE ID = {0}", iId);
            bool bRet = db.EjecutarSQL(sSQL);
            if (bRet)
                return 1;
            return iRet;
        }

    }
}
